package com.talent.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * Created by zhumenglong on 2017/5/16.
 */
@Controller
@RequestMapping("/formToken")
public class FormTokenController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取formtoken
     * @param request
     * @param operation 接下来要调用的接口名称eg:addSysUserAOP
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public String get(HttpServletRequest request,String operation){
        String sessionId = request.getSession().getId();
        ValueOperations<String, Object> stringObjectValueOperations = redisTemplate.opsForValue();
        HashOperations<String, Object, Object> stringObjectObjectHashOperations = redisTemplate.opsForHash();
        // key应该存储为会话标识
        UUID uuid = UUID.randomUUID();

        //stringObjectObjectHashOperations.put(sessionId,operation,uuid);
        stringObjectObjectHashOperations.put("sessionId",operation,uuid.toString());
        String value=(String)redisTemplate.opsForHash().get("sessionId",operation);

        //stringObjectValueOperations.set("formToken", uuid.toString());

        return operation+"_"+uuid;
    }
}
